﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComLib.ConfigService;
using LogLib;

namespace KAImage.Config
{
    public class AlgorithmApplyingOption : IniConfigBase
    {
        public bool Enabled
        {
            get
            {
                return (AdjustEnabled || Megre);
            }
        }

        public bool AdjustEnabled
        {
            get
            {
                return (_levels || _hsl || _curve || _blurSharpen);
            }
        }

        private bool _levels = false;
        public bool Levels
        {
            get { return _levels; }
            set { _levels = value; }
        }

        private bool _hsl = false;
        public bool HSL
        {
            get { return _hsl; }
            set { _hsl = value; }
        }

        private bool _megre = false;
        public bool Megre
        {
            get { return _megre; }
            set { _megre = value; }
        }

        private bool _curve = false;
        public bool Curve
        {
            get { return _curve; }
            set { _curve = value; }
        }

        private bool _blurSharpen = false;
        public bool BlurSharpen
        {
            get { return _blurSharpen; }
            set { _blurSharpen = value; }
        }

        public override bool Save()
        {
            base.Save();
            SaveField("HSL", _hsl);
            SaveField("BlurSharpen", _blurSharpen);
            SaveField("Curve", _curve);
            SaveField("Levels", _levels);
            SaveField("Megre", _megre);
            return true;
        }

        public override bool Load()
        {
            base.Load();
            try
            {
                _hsl = LoadBoolenValue("HSL");
                _blurSharpen = LoadBoolenValue("BlurSharpen");
                _curve = LoadBoolenValue("Curve");
                _levels = LoadBoolenValue("Levels");
                _megre = LoadBoolenValue("Megre");
            }
            catch (Exception ex)
            {
                ErrorMessage = string.Format("配置文件{0}加载错误:\n\t{1}", ConfigFilePath, ex.Message);
                Log.WriteException(ex.ToString());
                return false;
            }
            return true;
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder("算法应用选项:");
            Action<string, bool> FillString =
              (string field, bool val) =>
              {
                  builder.Append("[");
                  builder.Append(field);
                  builder.Append("]: ");
                  builder.Append(val ? "启用" : "不启用");
              };
            FillString("HSL]: ", _hsl);
            FillString("Curve]: ", _curve);
            FillString("Levels]: ", _levels);
            FillString("Megre]: ", _megre);
            FillString("BlurSharpen]: ", _blurSharpen);
            return builder.ToString();
        }
    }
}
